package com.example.handheld4_gsignal_app;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.example.handheld4_gsignal_app.ui.fragments.DashboardFragment;
import com.example.handheld4_gsignal_app.ui.fragments.HistoryFragment;
import com.example.handheld4_gsignal_app.ui.fragments.HistoryFragmentSimple;
import com.example.handheld4_gsignal_app.ui.fragments.TaskBoardFragment;
import com.example.handheld4_gsignal_app.ui.fragments.SettingsFragment;

/**
 * 主Activity - 4G信号检测系统主界面
 * 包含底部导航栏和四个主要功能模块
 */
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView bottomNavigationView;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 显示测试Toast
        android.widget.Toast.makeText(this, "应用启动成功！", android.widget.Toast.LENGTH_SHORT).show();

        initViews();
        setupBottomNavigation();
        showDefaultFragment();
    }

    /**
     * 初始化视图组件
     */
    private void initViews() {
        bottomNavigationView = findViewById(R.id.bottom_navigation);
    }

    /**
     * 设置底部导航栏
     */
    private void setupBottomNavigation() {
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    /**
     * 显示默认Fragment（实时监控）
     */
    private void showDefaultFragment() {
        if (currentFragment == null) {
            currentFragment = new DashboardFragment();
            loadFragment(currentFragment);
            // 自动选中"实时"页面
            bottomNavigationView.setSelectedItemId(R.id.nav_dashboard);
        }
    }

    /**
     * 底部导航栏点击事件处理
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        
        int itemId = item.getItemId();
        if (itemId == R.id.nav_dashboard) {
            android.util.Log.d("MainActivity", "Dashboard selected");
            fragment = new DashboardFragment();
        } else if (itemId == R.id.nav_history) {
            android.util.Log.d("MainActivity", "History selected - creating HistoryFragment");
            try {
                fragment = new HistoryFragment();
                android.util.Log.d("MainActivity", "HistoryFragment created successfully");
            } catch (Exception e) {
                android.util.Log.e("MainActivity", "Error creating HistoryFragment: " + e.getMessage(), e);
                return false;
            }
        } else if (itemId == R.id.nav_tasks) {
            android.util.Log.d("MainActivity", "Tasks selected");
            fragment = new TaskBoardFragment();
        } else if (itemId == R.id.nav_settings) {
            android.util.Log.d("MainActivity", "Settings selected");
            fragment = new SettingsFragment();
        }

        if (fragment != null) {
            android.util.Log.d("MainActivity", "Loading fragment: " + fragment.getClass().getSimpleName());
            loadFragment(fragment);
            return true;
        }
        return false;
    }

    /**
     * 加载Fragment
     */
    private void loadFragment(Fragment fragment) {
        if (fragment != null && !fragment.equals(currentFragment)) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, fragment);
            transaction.commit();
            currentFragment = fragment;
        }
    }

    /**
     * 启动信号采集Activity
     */
    public void startSignalCollectionActivity() {
        Intent intent = new Intent(this, SignalCollectionActivity.class);
        startActivity(intent);
    }
}